package com.cabrida.livestar.message;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.cabrida.livestar.BaseActivity;
import com.cabrida.livestar.R;
import com.cabrida.livestar.config.Global;
import com.cabrida.livestar.config.HttpHelp;
import com.cabrida.livestar.config.Urls;
import com.cabrida.livestar.model.BulletinData;
import com.cabrida.livestar.model.JSONParser;
import com.cabrida.livestar.model.LetterData;
import com.cabrida.livestar.model.MsgAdvanceData;
import com.cabrida.livestar.model.MsgLetterData;
import com.cabrida.livestar.model.WelfareData;
import com.cabrida.livestar.util.HttpUtils;
import com.cabrida.livestar.util.JsonUtils;
import com.cabrida.livestar.util.L;
import com.cabrida.livestar.util.NetworkUtils;
import com.loopj.android.http.JsonHttpResponseHandler;
import com.loopj.android.http.RequestParams;
/**
 * 消息中心
 */
public class MessageActivity extends BaseActivity {
	public static final String TAG = MessageActivity.class.getSimpleName();

	private ImageView iv_back;
	private TextView tv_title;
	
	/** * 公告 **/
	private ArrayList<BulletinData> data_list_bulletin = new ArrayList<BulletinData>();
	/** * 福利 **/
	private ArrayList<WelfareData> data_list_welfare = new ArrayList<WelfareData>();
	/** *预播 **/
	private ArrayList<MsgAdvanceData> data_list_advance = new ArrayList<MsgAdvanceData>();
	/** * 私信 **/
	private ArrayList<MsgLetterData> data_list_letter = new ArrayList<MsgLetterData>();
	/** * 留言 **/
	private ArrayList<LetterData> data_list_stay = new ArrayList<LetterData>();
	/** * 评论 **/
	private ArrayList<LetterData> data_list_comment = new ArrayList<LetterData>();
	
	private TextView tv_bulletin_time;
	private TextView tv_bulletin_des;
	private TextView tv_welfare_time;
	private TextView tv_welfare_des;
	private TextView tv_letter_time;
	private TextView tv_letter_des;
	private TextView tv_stay_time;
	private TextView tv_stay_des;
	private TextView tv_comment_time;
	private TextView tv_comment_des;
	
	private LinearLayout ll_notice;
	private LinearLayout ll_welfare;
	private LinearLayout ll_letter;
	private LinearLayout ll_leave_msg;
	private LinearLayout ll_comment;
	
	private ImageView iv_lv2;
	private TextView tv_lv2;
	
	private String type = Global.TYPE_FANS;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}
	
	private void setData(){
		if (null!=data_list_bulletin&&data_list_bulletin.size()>0) {
			tv_bulletin_time.setText(data_list_bulletin.get(0).getB_lasttime().toString());
			tv_bulletin_des.setText(data_list_bulletin.get(0).getB_description().toString());
		}
		if (type.equals(Global.TYPE_FANS)) {
			if (null!=data_list_welfare&&data_list_welfare.size()>0) {
				tv_welfare_time.setText(data_list_welfare.get(0).getO_createtime().toString());
				if ("10".equals(data_list_welfare.get(0).getO_type())) {
					tv_welfare_des.setText(data_list_welfare.get(0).getU_nickname()+"给你发红包啦");
				}else {
					tv_welfare_des.setText(data_list_welfare.get(0).getU_nickname()+"给你发礼品啦");
				}
			}
		}else {
			if (null!=data_list_advance&&data_list_advance.size()>0) {
				tv_welfare_time.setText(data_list_advance.get(0).getM_createtime().toString());
				tv_welfare_des.setText(data_list_advance.get(0).getM_title().toString());
			}
		}
		if (null!=data_list_letter&&data_list_letter.size()>0) {
			tv_letter_time.setText(data_list_letter.get(0).getC_createtime().toString());
			tv_letter_des.setText(data_list_letter.get(0).getCnickname()+"回复了我");
		}else {
			tv_letter_time.setText("");
			tv_letter_des.setText("暂无");
		}
		if (null!=data_list_stay&&data_list_stay.size()>0) {
			tv_stay_time.setText(data_list_stay.get(0).getC_createtime().toString());
			tv_stay_des.setText(data_list_stay.get(0).getU_nickname().toString()+"回复了我");
		}else {
			tv_stay_time.setText("");
			tv_stay_des.setText("暂无");
		}
		if (null!=data_list_stay&&data_list_stay.size()>0) {
			tv_stay_time.setText(data_list_stay.get(0).getC_createtime().toString());
			tv_stay_des.setText(data_list_stay.get(0).getU_nickname().toString()+"回复了我");
		}else {
			tv_stay_time.setText("");
			tv_stay_des.setText("暂无");
		}
		if (null!=data_list_comment&&data_list_comment.size()>0) {
			tv_comment_time.setText(data_list_comment.get(0).getC_createtime().toString());
			tv_comment_des.setText(data_list_comment.get(0).getU_nickname().toString()+"回复了我");
		}else {
			tv_comment_time.setText("");
			tv_comment_des.setText("暂无");
		}
	}

	@Override
	protected void findViewById() {
		// TODO Auto-generated method stub
		setContentView(R.layout.activity_message);
		iv_back = (ImageView) findViewById(R.id.iv_back);
		tv_title = (TextView) findViewById(R.id.tv_title);
		tv_bulletin_time = (TextView) findViewById(R.id.tv_bulletin_time);
		tv_bulletin_des = (TextView) findViewById(R.id.tv_bulletin_des);
		tv_welfare_time = (TextView) findViewById(R.id.tv_welfare_time);
		tv_welfare_des = (TextView) findViewById(R.id.tv_welfare_des);
		tv_letter_time = (TextView) findViewById(R.id.tv_letter_time);
		tv_letter_des = (TextView) findViewById(R.id.tv_letter_des);
		tv_stay_time = (TextView) findViewById(R.id.tv_stay_time);
		tv_stay_des = (TextView) findViewById(R.id.tv_stay_des);
		tv_comment_time = (TextView) findViewById(R.id.tv_comment_time);
		tv_comment_des = (TextView) findViewById(R.id.tv_comment_des);
		iv_lv2 = (ImageView) findViewById(R.id.iv_lv2);
		tv_lv2 = (TextView) findViewById(R.id.tv_lv2);
		ll_notice = (LinearLayout) findViewById(R.id.ll_notice);
		ll_welfare = (LinearLayout) findViewById(R.id.ll_welfare);
		ll_letter = (LinearLayout) findViewById(R.id.ll_letter);
		ll_leave_msg = (LinearLayout) findViewById(R.id.ll_leave_msg);
		ll_comment = (LinearLayout) findViewById(R.id.ll_comment);
	}

	@Override
	protected void initView() {
		// TODO Auto-generated method stub
		type = getIntent().getStringExtra("type");
		iv_back.setOnClickListener(this);
		ll_notice.setOnClickListener(this);
		ll_welfare.setOnClickListener(this);
		ll_letter.setOnClickListener(this);
		ll_leave_msg.setOnClickListener(this);
		ll_comment.setOnClickListener(this);
		tv_title.setText(getString(R.string.fans_msg));
		getMsgList();
		
		if (Global.TYPE_FANS.equals(type)) {
			iv_lv2.setImageResource(R.drawable.msg_advance);
			tv_lv2.setText("福利");
		}else {
			iv_lv2.setImageResource(R.drawable.msg_welfare);
			tv_lv2.setText("主播消息");
		}
	}
	
	/** 消息中心数据 */
	private void getMsgList() {
		// TODO Auto-generated method stub
		RequestParams params = new RequestParams();
		params.put("udid", Global.U_id);
		params.put("token", Global.U_token);
		if (type.equals(Global.TYPE_STAR)) {
			params.put("star", Global.TYPE_STAR);
		}
		if (!NetworkUtils.isNetAvailable(context)) {
			showToast(R.string.no_network);
			return;
		}
		showProgress();
		HttpUtils.getClient().post(Urls.GET_MSG_CENTER_URL, params,
				new JsonHttpResponseHandler() {

					@Override
					public void onSuccess(int statusCode, Header[] headers,
							JSONObject response) {
						dismissProgress();
						L.e(TAG, "getMsgList", response.toString());
						int code = JsonUtils.getInt(response, "code", -1);
						if (code == HttpHelp.Succeed) {
							JSONObject data = JsonUtils.getJSONObject(response,	"data", new JSONObject());
							JSONObject bulletin = JsonUtils.getJSONObject(data,	"bulletin", new JSONObject());
							JSONArray bulletin_list = JsonUtils.getJSONArray(bulletin, "list", new JSONArray());
							JSONObject obtain = JsonUtils.getJSONObject(data,	"obtain", new JSONObject());
							JSONArray obtain_list = JsonUtils.getJSONArray(obtain, "list", new JSONArray());
							JSONObject advance = JsonUtils.getJSONObject(data,	"advance", new JSONObject());
							JSONArray advance_list = JsonUtils.getJSONArray(advance, "list", new JSONArray());
							JSONObject letter = JsonUtils.getJSONObject(data,	"private", new JSONObject());
							JSONArray letter_list = JsonUtils.getJSONArray(letter, "list", new JSONArray());
							JSONObject stay = JsonUtils.getJSONObject(data,	"leave", new JSONObject());
							JSONArray stay_list = JsonUtils.getJSONArray(stay, "list", new JSONArray());
							JSONObject comment = JsonUtils.getJSONObject(data,	"comment", new JSONObject());
							JSONArray comment_list = JsonUtils.getJSONArray(comment, "list", new JSONArray());
							
							
							data_list_bulletin = JSONParser.getFansBulletinList(bulletin_list);
							data_list_welfare = JSONParser.getFansWelfareList(obtain_list);
							data_list_advance = JSONParser.getStarMsgAdvanceList(advance_list);
							data_list_letter = JSONParser.getFansLetterMsgList(letter_list);
							data_list_stay = JSONParser.getFansLetterList(stay_list);
							data_list_comment = JSONParser.getFansLetterList(comment_list);
							
							setData();
						} else {
							JSONObject data = JsonUtils.getJSONObject(response,	"data", new JSONObject());
							String message = JsonUtils.getString(data, "message", "");
							showToast(message);
						}
					}

					@Override
					public void onFailure(int statusCode, Header[] headers,
							String responseString, Throwable throwable) {
						dismissProgress();
						showToast(getResources().getString(R.string.fwq_error));
					}
				});
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		super.onClick(v);
		switch (v.getId()) {
		case R.id.iv_back:
			this.finish();
			break;
		case R.id.ll_notice:
			openActivity(NoticeActivity.class);
			break;
		case R.id.ll_welfare:
			if (type.equals(Global.TYPE_FANS)) {
				openActivity(WelfareActivity.class);
			}else {
				openActivity(AdvanceActivity.class);
			}
			break;
		case R.id.ll_letter:
		{
			Bundle b = new Bundle();
			if (type.equals(Global.TYPE_FANS)) {
				b.putString("type", Global.TYPE_FANS);
			}else {
				b.putString("type", Global.TYPE_STAR);
			}
			openActivity(LetterActivity.class,b);
		}
			break;
		case R.id.ll_leave_msg:
		{
			Bundle b = new Bundle();
			if (type.equals(Global.TYPE_FANS)) {
				b.putString("type", Global.TYPE_FANS);
			}else {
				b.putString("type", Global.TYPE_STAR);
			}
			openActivity(LeaveMsgActivity.class,b);
		}
			break;
		case R.id.ll_comment:
		{
			Bundle b = new Bundle();
			if (type.equals(Global.TYPE_FANS)) {
				b.putString("type", Global.TYPE_FANS);
			}else {
				b.putString("type", Global.TYPE_STAR);
			}
			openActivity(CommentActivity.class,b);
		}
			break;
		default:
			break;
		}
	}

}
